import { getBoundingClientRect } from '@/utils/node';
import type { ScrollViewOnScrollEvent } from '@uni-helper/uni-app-types';
import type { Ref } from 'vue';
import { ref, onMounted, nextTick, getCurrentInstance } from "vue"

export function useScrollTop() {
	const scrollTop = ref(0);
	function scrollEvent(event : ScrollViewOnScrollEvent) {
		scrollTop.value = event.detail.scrollTop;
	}
	return {
		scrollTop,
		scrollEvent
	}
}

export function useGetElementHeight(el : string) : [Ref<number>, () => void] {
	const height = ref(0);
	const currentInstance = getCurrentInstance();
	async function getElementHeight() {
		await nextTick();
		getBoundingClientRect(currentInstance, el).then((rect) => {
			height.value = rect?.height ?? 0;
		});
	}
	onMounted(async () => {
		await getElementHeight();
	})
	return [height, getElementHeight];
}